/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UINewMessage.java
 *
 * Created on 19-nov-2011, 14:48:46
 */
package mail.ui;

import javax.swing.JOptionPane;
import mail.attribute.MailItem;
import mail.client.MailClient;

/**
 *
 * @author Administrador
 */
public class UINewMessage extends javax.swing.JDialog implements MailSendForm {

    private MailItem item;
    private MailClient client;
    private MainScreen mainScreen;

    /** Creates new form UINewMessage */
    public UINewMessage(java.awt.Frame parent, boolean modal, MailClient client,MainScreen mainScreen) {
        super(parent, modal);
        this.client = client;
        this.mainScreen = mainScreen;
        initComponents();
        setLocationRelativeTo(null);
        init();        
    }
    
    protected void init(){
        item = new MailItem(client.getUserAccount().toString(), "", "", "");
        setItem(item);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        mailViewComponent1 = new mail.ui.MailViewComponent();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("SEND");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("CANCEL");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addContainerGap(429, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(mailViewComponent1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mailViewComponent1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    this.send();
    mainScreen.refresh();
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    this.cancel();
}//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private mail.ui.MailViewComponent mailViewComponent1;
    // End of variables declaration//GEN-END:variables

    @Override
    public boolean send() {
        boolean valid = validateForm();
        if (valid) {
            client.sendMailItem(item);
            JOptionPane.showMessageDialog(this, "Mensaje enviado");
            this.dispose();
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "Error al enviar el Correo.");
        }
        return valid;
    }

    @Override
    public boolean validateForm() {
        mailViewComponent1.updateData();

        String to = this.item.getFromTo().getTo().toString();
        String subject = this.item.getSubject();
        String body = this.item.getMessage().getMessage().toString();

        if (to.length() > 3) {
            if (subject.length() > 0 && !subject.contentEquals("\n")) {
                if (!body.isEmpty()) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean cancel() {
        if (JOptionPane.showConfirmDialog(this, "¿Desea cancelar el mensaje?", "Nuevo mensaje", JOptionPane.OK_OPTION, JOptionPane.OK_CANCEL_OPTION) == 0) {
            this.setVisible(false);
        }
        return true;
    }

    public void setItem(MailItem item) {
        this.item = item;
        this.mailViewComponent1.setMailItem(item, (byte) 1);
    }

    public MailItem getItem() {
        return item;
    }
}
